package xin.printk.android;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import xin.printk.android.utils.AppConstants;
import xin.printk.android.utils.PrefUtils;

public abstract class BasicActivity extends AppCompatActivity {

    private boolean canFinish = false;
    private Toolbar mToolbar;

    protected String[] mLevelItems;
    protected final int[] mLevelColorResId = {R.color.red_light, R.color.orange_light,
            R.color.yellow_light, R.color.gray_light};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.hide();
            }
        }
        mLevelItems = getResources().getStringArray(R.array.plan_level);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getSupportActionBar() != null && canFinish) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            if (mToolbar != null) {
                mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
                mToolbar.setBackgroundColor(getThemeColor());
            }
        }
    }

    public int getThemeColor() {
        return PrefUtils.getInt(AppConstants.THEME_COLOR, getResources().getColor(R.color.blue_light));
    }

    public String getThemeName() {
        return PrefUtils.getString(AppConstants.THEME_COLOR_NAME, getString(R.string.official_blue));
    }

    public void setToolbar(Toolbar toolbar) {
        setSupportActionBar(toolbar);
        mToolbar = toolbar;
    }

    public void setToolbar(@IdRes int toolbarId) {
        mToolbar = findViewById(toolbarId);
        setSupportActionBar(mToolbar);
    }

    public void setCanFinish(boolean canFinish) {
        this.canFinish = canFinish;
    }
}
